package cn.lbwfree.day3_25;

import java.util.Scanner;

/**
 * @date:2020年3月25日
 * @author:lbwfree
 */
public class ReturnGetNum {
	public static void main(String[] args) {
		Scanner inScanner = new Scanner(System.in);
		int m =inScanner.nextInt();
		int n =inScanner.nextInt();
		int[][] arrays = new int[m][n];
		int[][] temp = new int[m][n];
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < n; j++) {
				arrays[i][j]=inScanner.nextInt();
				temp[i][j]=1;
			}
		}
		//最外圈
		ReturnGetNumPrint(arrays, temp);
	}
	public static void ReturnGetNumPrint(int[][] arrays, int[][] temp) {
		//保证最后一位没有" "，所以" "+num;
		System.out.print(arrays[0][0]);
		temp[0][0]=0;
		int count=1;
		int i=1,j=0;
		int n=0;
		while(count<arrays.length*arrays[0].length)
		{
			//向下
			while(i<arrays.length&&temp[i][j]==1) {
				System.out.print(" "+arrays[i][j]);
				temp[i][j]=0;
				i++;
				count++;
			}
			i--;
			//左下角跳过重复
			j++;
			//向右
			while(j<arrays[0].length&&temp[i][j]==1) {
				System.out.print(" "+arrays[i][j]);
				temp[i][j]=0;
				j++;
				count++;
			}
			j--;
			//右下角跳过重复
			i--;
			//向上
			while(i>=0&&temp[i][j]==1) {
				System.out.print(" "+arrays[i][j]);
				temp[i][j]=0;
				i--;
				count++;
			}
			i++;
			//右上角跳过重复
			j--;
			//向左
			while(j>=0&&temp[i][j]==1) {
				System.out.print(" "+arrays[i][j]);
				temp[i][j]=0;
				j--;
				count++;
			}
			j++;
			//左上角跳过重复
			i++;
		}
	}

}
